package br.com.binding.rebind.template;

import br.com.binding.rebind.BoundModelClass;
import com.google.gwt.core.ext.typeinfo.JField;

public class Bound_ModelTemplate
{
  protected static String nl;
  public static synchronized Bound_ModelTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    Bound_ModelTemplate result = new Bound_ModelTemplate();
    nl = null;
    return result;
  }

  protected final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl; //$NON-NLS-1$
  protected final String TEXT_1 = " "; //$NON-NLS-1$
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = "\tprivate PropertyChangeSupport pcs;" + NL + NL + "\tpublic Bound_"; //$NON-NLS-1$ //$NON-NLS-2$
  protected final String TEXT_4 = "() {" + NL + "\t\tpcs = new PropertyChangeSupport(this);" + NL + "\t}" + NL + NL + "\t/**" + NL + "\t * @param listener" + NL + "\t * @see br.com.gwt.symbiosis.client.event.PropertyChangeSupport#addPropertyChangeListener(br.com.gwt.symbiosis.client.event.PropertyChangeListener)" + NL + "\t */" + NL + "\tpublic void addPropertyChangeListener(PropertyChangeListener listener) {" + NL + "\t\tpcs.addPropertyChangeListener(listener);" + NL + "\t}" + NL + NL + "\t/**" + NL + "\t * @param listener" + NL + "\t * @see br.com.gwt.symbiosis.client.event.PropertyChangeSupport#removePropertyChangeListener(br.com.gwt.symbiosis.client.event.PropertyChangeListener)" + NL + "\t */" + NL + "\tpublic void removePropertyChangeListener(PropertyChangeListener listener) {" + NL + "\t\tpcs.removePropertyChangeListener(listener);" + NL + "\t}" + NL + "\t" + NL; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$ //$NON-NLS-15$ //$NON-NLS-16$ //$NON-NLS-17$ //$NON-NLS-18$
  protected final String TEXT_5 = "\tpublic void "; //$NON-NLS-1$
  protected final String TEXT_6 = "("; //$NON-NLS-1$
  protected final String TEXT_7 = " "; //$NON-NLS-1$
  protected final String TEXT_8 = ") {" + NL + "\t\tObject old = "; //$NON-NLS-1$ //$NON-NLS-2$
  protected final String TEXT_9 = "();" + NL + "\t\tsuper."; //$NON-NLS-1$ //$NON-NLS-2$
  protected final String TEXT_10 = "("; //$NON-NLS-1$
  protected final String TEXT_11 = ");" + NL + "\t\tpcs.firePropertyChange(\""; //$NON-NLS-1$ //$NON-NLS-2$
  protected final String TEXT_12 = "\", old, "; //$NON-NLS-1$
  protected final String TEXT_13 = ");" + NL + "\t}" + NL + NL; //$NON-NLS-1$ //$NON-NLS-2$
  protected final String TEXT_14 = "}"; //$NON-NLS-1$

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(TEXT_2);
 
	BoundModelClass modelClass = (BoundModelClass)argument;
	JField[] fields = modelClass.getFields();

    stringBuffer.append(TEXT_3);
    stringBuffer.append(modelClass.getClassName());
    stringBuffer.append(TEXT_4);
 for(JField field : fields) { 

		String propertyName = field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
		String setMethod = "set" + propertyName;
		String getMethod = "get" + propertyName;
	
    stringBuffer.append(TEXT_5);
    stringBuffer.append(setMethod);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(field.getType().getQualifiedSourceName());
    stringBuffer.append(TEXT_7);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_8);
    stringBuffer.append(getMethod);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(setMethod);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_11);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(field.getName());
    stringBuffer.append(TEXT_13);
}
    stringBuffer.append(TEXT_14);
    return stringBuffer.toString();
  }
}
